package org.arduomo.serial.ui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.arduomo.serial.controller.RelayController;
import org.arduomo.serial.controller.SerialController;


public class RelayControlPanel extends JPanel {
	public RelayControlPanel() {
		super();
		initialize();
	}

	private void initialize() {
		setLayout(new GridLayout(4, 1));
		for (int i = 0; i < 4; i++) {
			JButton button = new JButton();
			button.setText("Relay "+i);
			button.setActionCommand("switch:"+i);
			button.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent ev) {
					String message = ev.getActionCommand();
					System.out.println("actionPerformed - "+message);
					try {
						SerialController.getInstance().sendMessage(new String(message) );
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			});
			add(button);
		}
	}

	public static void main(String[] args) {
		JFrame f = new JFrame();
		RelayControlPanel relayControlPanel = new RelayControlPanel();
		f.setContentPane(relayControlPanel);
		f.setSize(new Dimension(400, 300));
		f.setLocationRelativeTo(null);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}
}
